// pages/user/invoice/to-invoice/to-invoice.js
import { $wuxSelect } from '../../../../wux-components/index'
const config = getApp().globalData.config
const T = getApp().globalData.T
let token = ''
let userId = -1
Page({

  /**
   * 页面的初始数据
   */
  data: {
    item:{title:'请选择'},
    input01:'',
    input02:'',
    input03:'',
    input04:'',
    input05:'',
    input06:'',
    input07:'',
    input08:'',
    inType: ['企业增值税普通发票', , '增值税专用发票', '组织(非企业)增值税普通发票'], //发票类型
    defaultImg: '/resource/image/checkbox_normal.png',
    active:'/resource/image/checkbox_selected.png',
    kind: -1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    if (getApp().globalData.role == 1) { // 超管
      this.setData({
        isAdmin: true
      })
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    token = getApp().globalData.token;
    userId = getApp().globalData.userId;
    this.getDetail();
  },

  onInput01(e) {
    this.setData({input01: e.detail.value})
  },
  onInput02(e) {
    this.setData({input02: e.detail.value})
  },
  onInput03(e) {
    this.setData({input03: e.detail.value})
  },
  onInput04(e) {
    this.setData({input04: e.detail.value})
  },
  onInput05(e) {
    this.setData({input05: e.detail.value})
  },
  onInput06(e) {
    this.setData({input06: e.detail.value})
  },
  onInput07(e) {
    this.setData({input07: e.detail.value})
  },
  onInput08(e) {
    this.setData({input08: e.detail.value})
  },

  onClickInvoice() {
    $wuxSelect('#wux-select-invoice').open({
      value: '',
      options: [
        { title:'企业增值税普通发票', value: 0},
        { title:'增值税专用发票', value: 2},
        { title:'组织(非企业)增值税普通发票', value: 3}
      ],
      onConfirm: (value, index, options) => {
        if (index !== -1) {
          this.setData({
            value: value,
            item: options[index],
          })
        }
      }
    });
  },

  getDetail() {
    T.req({
      loadTitle: '初始化中...',
      url: config.api + 'invoice/info',
      method: 'GET',
      data: {
        token: token
      },
      success: (res) => {
        this.setData({
          input01: res.fullName,
          input02: res.taxpayerId,
          input03: res.mobile,
          input04: res.address,
          input05: res.email,
          input06: res.bankName,
          input07: res.bankNo,
          input08: res.unionPayNo,
          item: { title: this.data.inType[res.invoiceType], value: res.invoiceType},
          kind: res.classify == 0 ? 1 : 2,
        })
      }
    })
  },

  onSelectKindType(e) {
    let ky = e.currentTarget.dataset.index;
    this.setData({
      kind: ky
    })
  },

  onKeepEdit() {
    let obj = {};
    obj.fullName = this.data.input01;
    obj.taxpayerId = this.data.input02;
    obj.mobile = this.data.input03;
    obj.address = this.data.input04;
    obj.email = this.data.input05;
    obj.bankName = this.data.input06;
    obj.bankNo = this.data.input07;
    obj.unionPayNo = this.data.input08;
    obj.invoiceType = this.data.item;
    obj.classify = Number(this.data.kind);
    obj.userHrId = userId;
    let isError = false;
    console.log(obj);
    for(const k in obj) {
      if(!obj[k] || obj[k].title == '请选择') {
        isError = true;
      }
    }
    if (isError) {
      T.toast({ id: '#toast', text: '还有选项没有填写或者输入,请检查' });
      return ;
    }

    if (!T.regExp.tel.test(obj.mobile)) {
      T.toast({ id: '#toast', text: '请输入正确的联系电话' });
      return ;
    }

    if (!T.regExp.email.test(obj.email)) {
      T.toast({ id: '#toast', text: '请输入正确的邮箱' });
      return ;
    }


    obj.invoiceType = this.data.item.value;
    obj.classify  = obj.classify -1;
    console.log(obj)
    T.req({
      loadTitle: '请求中...',
      url: config.api + 'invoice/info?token=' + token,
      method: 'POST',
      data: obj,
      success: (res) => {
        T.toast({ id: '#toast', text: '编辑成功', success: () => {
          wx.navigateBack({
            delta: 1
          })
        } });
      }
    })
  }
})